﻿using CommunityToolkit.Mvvm.ComponentModel;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;

namespace Vision.VHelper
{
    public class ParamData : ObservableObject, IEditableObject
    {
        public string Name { get; set; }
        public string DisplayName { get; set; }

        private object _value;
        public object Value
        {
            get => _value;
            set
            {
                if (SetProperty(ref _value, value))
                {
                    OnValueChanged(new ValueChangedEventArgs(this));
                }
            }
        }

        // 定义事件
        public event EventHandler<ValueChangedEventArgs> ValueChanged;

        // 触发事件的方法
        protected virtual void OnValueChanged(ValueChangedEventArgs e)
        {
            ValueChanged?.Invoke(this, e);
        }


        private object _backupValue;

        public void BeginEdit()
        {
            _backupValue = _value;
        }

        public void EndEdit()
        {
            _backupValue = null;
        }

        public void CancelEdit()
        {
            Value = _backupValue;
        }
    }

    public class IntParamData : ParamData
    {
        public new int Value
        {
            get => (int)base.Value;
            set => base.Value = value;
        }
    }

    public class FloatParamData : ParamData
    {
        public new float Value
        {
            get => (float)base.Value;
            set => base.Value = value;
        }
    }

    public class EnumParamData : ParamData
    {
        public ObservableCollection<string> EnumRange { get; set; }
        public Dictionary<string, string> ParamValue { get; set; }
        public new string Value
        {
            get => (string)base.Value;
            set => base.Value = value;
        }

        // 添加构造函数来初始化 ParamValue 字典
        public EnumParamData(IEnumerable<(string DisplayName, string Name)> enumValues)
        {
            EnumRange = new ObservableCollection<string>(enumValues.Select(ev => ev.DisplayName));
            ParamValue = enumValues.ToDictionary(ev => ev.DisplayName, ev => ev.Name);
        }

    }

    public class BooleanParamData : ParamData
    {
        public ObservableCollection<string> EnumRange { get; set; } = new ObservableCollection<string> { "True", "False" };
        public new string Value
        {
            get => (string)base.Value;
            set => base.Value = value;
        }
    }

    public class ValueChangedEventArgs : EventArgs
    {
        public ParamData ParamData { get; }

        public ValueChangedEventArgs(ParamData paramData)
        {
            ParamData = paramData;
        }
    }

    public class ValueTemplateSelector : DataTemplateSelector
    {
        public DataTemplate TextBoxTemplate { get; set; }
        public DataTemplate ComboBoxTemplate { get; set; }

        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            if (item is IntParamData)
            {
                return TextBoxTemplate;
            }
            if (item is FloatParamData)
            {
                return TextBoxTemplate;
            }
            if (item is EnumParamData)
            {
                return ComboBoxTemplate;
            }
            return base.SelectTemplate(item, container);
        }
    }
}
